/**
 * <p>Title: ServiceZMCF27.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-9-23 下午05:03:11  张志芳  Create
 *</pre>
 */
package com.baosight.sczx.zm.cg.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.zm.cg.domain.ZMCGCallRecord;
import com.baosight.sczx.zm.cg.domain.ZMCGContactList;
import com.baosight.sczx.zm.cg.domain.ZMCGContactReceive;
import com.baosight.sczx.zm.ch.domain.ZMCHProduceStatistics;
import com.baosight.sczx.zm.cn.domain.ZMCNMccyMain;


/**
 * 接收部门
 * <p>Title: ServiceZMCF27.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-9-23 下午05:03:11  张志芳  Create
 *</pre>
 */
public class ServiceZMCG2002 extends ServiceEPBase {
	/**
	 * 初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:56:26 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(Department.getInstance().getConstructionUnit(false));//部门
		return outInfo;
	}

	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:56:44 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdContactlistId = inInfo.getString("fdContactlistId");
		EiInfo outInfo = inInfo;
		if(fdContactlistId!=null)
		{ 
			StaticTools.setQueryParameters(inInfo, "fdContactlistId", fdContactlistId, true);//添加查询条件
			outInfo = super.query(inInfo, "ZMCGContactReceive.query", new ZMCGContactReceive());
		}else
		{
			outInfo.addBlock("result").setBlockMeta(new ZMCGContactReceive().eiMetadata);
				
		}
//		String fdFeedbackDepartId = UserSession.getInSessionProperty("orgCode").toString();//发送部门
//		StaticTools.setQueryParameters(inInfo, "fdFeedbackDepartId", fdFeedbackDepartId, true);//添加查询条件
	    outInfo = super.query(inInfo, "ZMCGContactReceive.query", new ZMCGContactReceive());
		outInfo.addBlock(Department.getInstance().getConstructionUnit(false));//部门	
//		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));
		
		
		return outInfo;
	}
	
	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:56:59 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings("unchecked")
	@Override
	public EiInfo insert(EiInfo inInfo) {
		String fdContactlistId = inInfo.getString("fdContactlistId");
		EiInfo out=new EiInfo(); 
		String fdStatus = "未反馈";
		ZMCGContactReceive _dom = new ZMCGContactReceive();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		List<ZMCGContactReceive> list = null;
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
//			String id = _dom.getFdContactlistId();
//			Map<String, String> map = new HashMap<String, String>();
			Map<String, String> params = new HashMap<String, String>();
			
			
			boolean validate = validate(inInfo, _dom,"ZMCGContactReceive.validate",params);
			if(validate) {
				inInfo = this.query(inInfo);
				return inInfo;
			}else{
				
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CG_CONTACTRECEIVE"));
			_dom.setFdStatus(fdStatus);
			_dom.setFdContactlistId(fdContactlistId);
			_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());	//矿井id
			this.dao.insert("ZMCGContactReceive.insert",_dom);
		}
	}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
		
		
//		List<ZMCGContactReceive> list = null;
//		for(int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++)
//		{
//			//判断部门是否重复
//			if(list == null)
//			{
//				String id = inInfo.getBlock(EiConstant.resultBlock).getCellStr(i, "fdContactlistId");
//				Map<String, String> map = new HashMap<String, String>();
//				map.put("fdContactlistId", id);
//				list = dao.query("ZMCGContactReceive.query", map);
//			}
//			for(ZMCGContactReceive tmp : list)
//			{
//				if(tmp.getFdReceiveId().equals(inInfo.getBlock(EiConstant.resultBlock).getCellStr(i, "fdReceiveId")))
//				{
//					inInfo.setStatus(-1);
//					inInfo.setMsg("不能重复添加，请去掉已添加过的接收部门！");
//					return inInfo;
//				}
//			}
//			
//			inInfo.getBlock(EiConstant.resultBlock).setCell(i, "fdKeyId", TablePk.getInstance().getNextValue("T_CF_CONTACTRECEIVE"));			
//		}		
//		EiInfo outinfo = super.insert(inInfo, "ZMCGContactReceive.insert");
//		return outinfo;
//	}
//	
	
	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ZMCG20</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-24上午13:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo();
		ZMCGContactReceive _dom = new ZMCGContactReceive();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());

		    this.dao.update("ZMCGContactReceive.update", _dom);			
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
		
		
		
}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:57:29 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {		
		EiInfo out=new EiInfo(); 
		ZMCGContactReceive _dom = new ZMCGContactReceive();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
		    this.dao.delete("ZMCGContactReceive.delete",_dom);	
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
	
	
	
	/**
	 * 校验是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:48:34 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	private boolean validate(EiInfo inInfo, ZMCGContactReceive _dom, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		String fdReceiveId = _dom.getFdReceiveId();
		String fdTitle = _dom.getFdTitle();
		params.put("fdReceiveId", fdReceiveId);
		params.put("fdTitle", fdTitle);
		List result = dao.query("ZMCGContactReceive.validate", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "每条通知每部门只能发送一次！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	
	}	
	
}
		